package testDemo.Thread.SellTicketTest;

public class SellTicket implements Runnable {
    private int ticket=100;
    private int n=0;
//    private Lock lock = new ReentrantLock();
    @Override
    public void run() {
        while(true){
            synchronized(SellTicket.class){
                sell();
            }
        }
    }
    private void sell() {
        if(ticket>0){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"正在出售第："+ticket+"张票");
            ticket--;
        }
    }
//    private void sell() {
//        lock.lock();
//        if(ticket>0){
//            try {
//                Thread.sleep(100);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            System.out.println(Thread.currentThread().getName()+"正在出售第："+ticket+"张票");
//            ticket--;
//        }
//        lock.unlock();
//    }
}
